/**
 * 
 */
package info.niwota.dc.bg;

import info.niwota.dc.Log;
import info.niwota.dc.Preferences;
import info.niwota.dc.R;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.ImageColumns;

/**
 * @author qiangli
 * 
 */
public class PickImageActivity extends Activity {

	private static final int DIALOG_SELECT = 1;

	private static final int RC_PICK = 1;

	private static final int RC_SLIDES = 2;

	private void doPick() {
		try {
			Intent i = new Intent(Intent.ACTION_GET_CONTENT);
			i.setType("image/*");
			startActivityForResult(i, RC_PICK);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doSelect(int which) {
		switch (which) {
		case Preferences.BG_SELECT_PICK:
			doPick();
			return;
		case Preferences.BG_SELECT_NO_BACKGROUND:
			break;
		case Preferences.BG_SELECT_GONE_WILD:
			break;
		case Preferences.BG_SELECT_WALLPAPER:
			break;
		case Preferences.BG_SELECT_SLIDES:
			doSelectSlides();
			return;
		default:
		}
		//selection except pick/slides
		Preferences.putBackgroundSelection(this, which);
		finish();
	}

	private void doSelectSlides() {
		try {
			Intent i = new Intent(this, MultiSelectImageActivity.class);
			startActivityForResult(i, RC_SLIDES);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Log.LOGV) {
			Log.v("onActivityResult requestCode: " + requestCode
					+ " resultCode: " + resultCode + " == " + RESULT_OK
					+ " data: " + data);
		}
		if (resultCode == RESULT_CANCELED) {
			setResult(RESULT_CANCELED);
			finish();
			return;
		}
		try {
			switch (requestCode) {
			case RC_PICK: {
					Uri uri = data.getData();
					if (uri != null) {
						Cursor c = getContentResolver().query(uri,
								new String[] { ImageColumns.DATA }, null, null,
								null);
						c.moveToFirst();
						String p = c.getString(0);
						c.close();
						Preferences.putBackgroundSelection(this, Preferences.BG_SELECT_PICK);
						Preferences.putBackgroundImagePath(this, p);
					}
				}
				break;
			case RC_SLIDES: {
				Preferences.putBackgroundSelection(this, Preferences.BG_SELECT_SLIDES);
				List<Uri> list = data.getParcelableArrayListExtra(MultiSelectImageActivity.EXTRA_URIS);
				ArrayList<String> uris = new ArrayList<String>();
				for (Uri uri: list) {
					uris.add(uri.toString());
				}
				Preferences.putUris(this, uris.toArray(new String[0]));
				}
				break;
			default:
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setResult(RESULT_OK);
		finish();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		showDialog(DIALOG_SELECT);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SELECT:
			int checked = Preferences.getBackgroundSelection(this);
			return new AlertDialog.Builder(this).setTitle(
					R.string.bg_pick_title).setSingleChoiceItems(
					R.array.compose_bg_select_entries, checked,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							doSelect(which);
						}
					}).setCancelable(true).setOnCancelListener(
					new OnCancelListener() {
						public void onCancel(DialogInterface arg0) {
							finish();
						}
					}).create();
		default:
		}
		return super.onCreateDialog(id);
	}
}
